from selenium.webdriver.common.by import By
from pages.base_page import Page
from selenium.webdriver.support.wait import WebDriverWait


class ChatVolumePage(Page):
    # Type
    ele_title = (By.ID, u'page-chatVolumeByTime-title')

    ele_by_time = (By.XPATH, u'//div/span[text() = "By Time"]/..')
    ele_by_department = (By.XPATH, u'//div/span[text() = "By Department"]/..')
    ele_by_segment = (By.XPATH, u'//div/span[text() = "By Segment"]/..')
    ele_by_campaign = (By.XPATH, u'//div/span[text() = "By Campaign"]/..')

    ele_by_time_form = (By.XPATH, u'//div/canvas')
    ele_by_time_chat_volume_details_table = (By.XPATH, u'//thead/tr')

    ele_by_department_chats = (By.XPATH, u'//div[text()="Chats"]')
    ele_by_department_chat_acceptance_rate = (By.XPATH, u'//div[text()="Chat Acceptance Rate"]')
    ele_by_department_missed_chats = (By.XPATH, u'//div[text()="Missed Chats"]')
    ele_by_department_refused_chats = (By.XPATH, u'//div[text()="Refused Chats"]')
    ele_by_department_chats_requests = (By.XPATH, u'//div[text()="Chat Requests"]')
    ele_by_department_chat_volume_details = (By.XPATH, u'//div[text()="Chat Volume Details - By Department"]')

    ele_by_segment_table = (By.XPATH, u'//table/thead/..')

    ele_by_campaign_chats = (By.XPATH, u'//div[text()="Chats"]')
    ele_by_campaign_chat_acceptance_rate = (By.XPATH, u'//div[text()="Chat Acceptance Rate"]')
    ele_by_campaign_missed_chats = (By.XPATH, u'//div[text()="Missed Chats"]')
    ele_by_campaign_refused_chats = (By.XPATH, u'//div[text()="Refused Chats"]')
    ele_by_campaign_chats_requests = (By.XPATH, u'//div[text()="Chat Requests"]')
    ele_by_campaign_visits = (By.XPATH, u'//div[text()="Visits"]')
    ele_by_campaign_avg_chat_time = (By.XPATH, u'//div[text()="Avg. Chat Time"]')
    ele_by_campaign_avg_wait_time = (By.XPATH, u'//div[text()="Avg. Wait Time"]')
    ele_by_campaign_chat_volume_details = (By.XPATH, u'//div[text()="Chat Volume Details - By Campaign - All Campaigns"]')

    ele_error = (By.XPATH, '//div[@role="alert"]')

    # No feature
    ele_no_feature_title = (By.ID, u'undefined-title')
    ele_no_feature_image = (By.XPATH, u'//div/h3[text()="Feature Introduction"]/../div/img')
    ele_unlock_feature_button = (By.XPATH, u'//button/span[text()="Chat with us"]/..')

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    def check_page_has_error(self):
        try:
            flag = self.find_element(*self.ele_error).is_enabled()
            if flag:
                raise Exception("current page {} may has error".format(self.url))
        except Exception as e:
            return True

    def chat_volume_page_eles_check_when_has_2_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_by_time).is_enabled()
        self.find_element(*self.ele_by_department).is_enabled()
        self.find_element(*self.ele_by_segment).is_enabled()
        self.find_element(*self.ele_by_campaign).is_enabled()
        self.find_element(*self.ele_by_time_form).is_enabled()
        self.find_element(*self.ele_by_time_chat_volume_details_table).is_enabled()
        self.click(self.ele_by_department)
        self.find_element(*self.ele_by_department_chats).is_enabled()
        self.find_element(*self.ele_by_department_chat_acceptance_rate).is_enabled()
        self.find_element(*self.ele_by_department_missed_chats).is_enabled()
        self.find_element(*self.ele_by_department_refused_chats).is_enabled()
        self.find_element(*self.ele_by_department_chats_requests).is_enabled()
        self.find_element(*self.ele_by_department_chat_volume_details).is_enabled()
        self.click(self.ele_by_segment)
        WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath(u'//table/thead/..'))
        self.find_element(*self.ele_by_segment_table).is_enabled()
        self.click(self.ele_by_campaign)
        WebDriverWait(self.driver, 10).until(lambda x: x.find_element_by_xpath(u'//div[text()="Chats"]'))
        self.find_element(*self.ele_by_campaign_chats).is_enabled()
        self.find_element(*self.ele_by_campaign_chat_acceptance_rate).is_enabled()
        self.find_element(*self.ele_by_campaign_missed_chats).is_enabled()
        self.find_element(*self.ele_by_campaign_refused_chats).is_enabled()
        self.find_element(*self.ele_by_campaign_chats_requests).is_enabled()
        self.find_element(*self.ele_by_campaign_visits).is_enabled()
        self.find_element(*self.ele_by_campaign_avg_chat_time).is_enabled()
        self.find_element(*self.ele_by_campaign_avg_wait_time).is_enabled()
        self.find_element(*self.ele_by_campaign_chat_volume_details).is_enabled()

    def chat_volume_page_eles_check_when_has_1_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_by_time).is_enabled()
        self.find_element(*self.ele_by_department).is_enabled()
        self.find_element(*self.ele_by_campaign).is_enabled()
        self.find_element(*self.ele_by_time_form).is_enabled()
        self.find_element(*self.ele_by_time_chat_volume_details_table).is_enabled()
        self.click(self.ele_by_department)
        self.find_element(*self.ele_by_department_chats).is_enabled()
        self.find_element(*self.ele_by_department_chat_acceptance_rate).is_enabled()
        self.find_element(*self.ele_by_department_missed_chats).is_enabled()
        self.find_element(*self.ele_by_department_refused_chats).is_enabled()
        self.find_element(*self.ele_by_department_chats_requests).is_enabled()
        self.find_element(*self.ele_by_department_chat_volume_details).is_enabled()
        self.click(self.ele_by_campaign)
        self.find_element(*self.ele_by_campaign_chats).is_enabled()
        self.find_element(*self.ele_by_campaign_chat_acceptance_rate).is_enabled()
        self.find_element(*self.ele_by_campaign_missed_chats).is_enabled()
        self.find_element(*self.ele_by_campaign_refused_chats).is_enabled()
        self.find_element(*self.ele_by_campaign_chats_requests).is_enabled()
        self.find_element(*self.ele_by_campaign_visits).is_enabled()
        self.find_element(*self.ele_by_campaign_avg_chat_time).is_enabled()
        self.find_element(*self.ele_by_campaign_avg_wait_time).is_enabled()
        self.find_element(*self.ele_by_campaign_chat_volume_details).is_enabled()

    def chat_volume_page_eles_check_when_no_feature(self):
        self.find_element(*self.ele_no_feature_title).is_enabled()
        self.find_element(*self.ele_no_feature_image).is_enabled()
        self.find_element(*self.ele_unlock_feature_button).is_enabled()



